/*                   _   _ _ __                               *\
**     ________ ___ | | | | '_ \   scaup - Scala up!          **
**    / __/ __// _ || | | | |_) |  (c) 2008, Normen Mueller   **
**  __\ \/ /__/ __ || |_| | .__/   http://kwarc.info.nmueller **
** /____/\___/_/ |_/ \__,_|_|                                 **
**                                                            **
\*                                                            */
  
// $Id: IOUtil.scala 7 2008-05-20 14:46:36Z normen.mueller $

package scaup.io.fs

import java.io.{IOException, FileNotFoundException}
import java.nio.charset._

object FileUtil {
  
  private val reader: SourceReader = {
    val charset =
      try {
        Charset.forName("UTF8")
      } catch {
        case _: IllegalCharsetNameException => error("illegal charset name 'UTF8'")
        case _: UnsupportedCharsetException => error("unsupported charset 'UTF8'")
      }
      new SourceReader(charset.newDecoder())
  }
  
  def getSourceFile(f: AbstractFile): SourceFile = new StaticSourceFile(f, reader.read(f))
    
  def getSourceFile(name: String): SourceFile = {
    val f = AbstractFile.getFile(name)
    if (f eq null) throw new FileNotFoundException("source file '" + name + "' could not be found")
    getSourceFile(f)
  }
  
  def mimetype(source: AbstractFile): String = new javax.activation.MimetypesFileTypeMap().getContentType(source.file)
  def mimetype(file: java.io.File): String = mimetype(AbstractFile.getFile(file))
}
